package com.atguigu.java;

import org.junit.Test;

import java.io.*;

/**
 * 测试FileInputStream 和 FileOutputStream 的区别
 * <p>
 * 结论：
 * 对于文本文件（.txt,.java,.c,.c++） 使用字符流来处理
 * 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt) 使用字节流来处理
 *
 * @author shkstart
 * @create 2021-01-27 16:23
 */
public class FileInOutStreamTest {
    /*
     * 实现文本图片的传入传出
     *
     * */
    @Test
    public void test1() {

        long start = System.currentTimeMillis();
        coplyFile("Capture001.png", "Capture004.png");
        long end = System.currentTimeMillis();
        System.out.println("总时长；" + (end - start));

    }

    /*
     * 抽离成一个方法
     *
     * */
    private void coplyFile(String upPath, String downPath) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1.创建文件对象
            File file = new File(upPath);
            File file1 = new File(downPath);

            //2.创建输入输出流对象
            fileInputStream = new FileInputStream(file);
            fileOutputStream = new FileOutputStream(file1);


            //3.输入输出操作
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
//                for (int i = 0; i < len; i++) {
//                    System.out.println(bytes[i]);
//
//                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭操作
            try {
                assert fileInputStream != null;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     *
     *实现图片加密操作
     * */
    @Test
    public void test2() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //1.创建File类的对象
            File file = new File("Capture001.png");
            File file1 = new File("Capture005.png");
            //2.创建流的对象
            fileInputStream = new FileInputStream(file);
            fileOutputStream = new FileOutputStream(file1);

            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.输入输出操作
            int len;
            byte[] bytes = new byte[10];
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                for (int i = 0; i < len; i++) {
                    bytes[i] = (byte) (bytes[i] ^ 5);

                }
                bufferedOutputStream.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }

}
